package com.multimedia.scala.euler

import scala.collection.mutable.ListBuffer
import scala.annotation.tailrec
/*
 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
 */
object Asd10 {
  def main(args : Array[String]) : Unit = {
      val max = 2000000
      val percent = max / 100
      var cur = 0
      var per = 0
      
      def findSimples(max: Int): scala.collection.mutable.ListBuffer[Int] = {
        var simples = ListBuffer[Int]()
        //var simplesSkip = scala.collection.mutable.HashMap[Int, Int]()
        @tailrec
        def findNextSimple(x: Int = 2): ListBuffer[Int] = {
            /*def findNextStep(step: Int = 1): Int = {
	          println(x, step, simplesSkip)
              simplesSkip.foreach{case (i, e) => {
                simplesSkip(i) = if (e==0) i-1 else e-1
              }}

			  if (simplesSkip.exists{case (i, e) => {e==0}}) findNextStep(step + 1)
			  else step
            }*/
	        cur += 1
	        //println(cur)
	        cur = if (cur%percent==0) {
	          per += 1
	          println(per+"%", simples.size)
	          0
	        } else {
	          cur
	        }
	        if (max<=x) return simples

	        var sqrtX = Math.sqrt(x).intValue
		    if (!simples.takeWhile(sqrtX>=).exists(y => x%y==0)) {
		        simples += x
		        //simplesSkip += x
		    }
		    //findNextSimple(x + findNextStep())
	        findNextSimple(x + 1)
        }
        findNextSimple()
      }
	  //take 2*n+1 digits - eliminates all that are divisible by 2
	  println(findSimples(max).map(BigInt(_)).sum)
      //println(5%3)
  }
}
